﻿using System.Collections.Generic;
using System.Web.Configuration;
using Store.Web.Models;

namespace Store.Web.Services
{
    public class StoreService : IStoreService
    {
        #region Private Fields

        private readonly string _connectionString;
        private readonly ProductService _productService;
        private readonly OrderService _orderService;
        private readonly CustomerService _customerService;
        private readonly PushService _pushService;

        #endregion

        #region Ctors

        public StoreService()
        {
            _connectionString = WebConfigurationManager.ConnectionStrings["StoreDb"].ConnectionString;
            _productService = new ProductService(_connectionString);
            _orderService = new OrderService(_connectionString);
            _customerService = new CustomerService(_connectionString);
            _pushService = new PushService(_connectionString);
        }

        #endregion

        #region Public Methods

        #region Products

        public List<Models.Product> GetHomeProducts(int number)
        {
            return _productService.GetProducts(number);
        }

        public List<Models.Product> GetProducts()
        {
            return GetHomeProducts(-1);
        }

        public Product GetProduct(int id)
        {
            return _productService.GetProduct(id);
        }

        #endregion


        #region Orders

        public List<Order> GetHomeOrders(int customerId, int number)
        {
            return _orderService.GetCommands(customerId, number);
        }

        public List<Order> GetOrders(int customerId)
        {
            return GetHomeOrders(customerId, -1);
        }

        public Order GetOrder(int id)
        {
            return _orderService.GetOrder(id);
        }

        public void SubmitOrder(Order command)
        {
            _orderService.SubmitCommand(command);
        }

        public void UpdateOrder(Order command)
        {
            _orderService.UpdateOrderState(command.Id, command.State);
        }

        #endregion

        #region Customers

        public Customer GetCustomer(int id)
        {
            return _customerService.GetCustomer(id);
        }

        public Customer Authenticate(string email, string password)
        {
            return _customerService.Authenticate(email, password);
        }

        public void AddCustomerAddress(int customerId, Address address)
        {
            _customerService.AddAddress(customerId, address);
        }

        #endregion

        public void SubscribeToPush(int customerId, string url)
        {
            _pushService.Subscribe(customerId, url);
        }

        public void UnsubscribeFromPush(string url)
        {
            _pushService.Unsubscribe(url);
        }

        public int GetOrderState(int orderId)
        {
            return _orderService.GetOrderState(orderId);
        }

        #endregion
    }
}
